package com.boot.utils;

import com.boot.entity.User;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * @author Chunsheng.Zhang
 * @date 2021/4/16 18:12
 * @description : TODO
 */
public class ContextUtil {

    public static String getCurrentUser(){

        String username = null;

        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (principal instanceof User){
            username = ((User) principal).getUsername();
        }else if(principal instanceof String){
            username = (String)principal;
        }else {
            username = "匿名";
        }

        return username;
    }


}
